package com.yingside.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("请输入老师的姓名:");
        String name = input.next();
        System.out.println("请输入老师的生日(yyyy-MM-dd):");
        String birthday = input.next();


        //1.导入mysql的jar包
        //2.程序导入mysql驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection conn = null;
        Statement stmt = null;

        String url = "jdbc:mysql://47.108.158.110:3306/j181db";
        String user = "root";
        String password = "123456";

        try {
            //3.获取数据库连接 三个参数：数据库连接的地址，数据库用户名，数据库密码
            conn = DriverManager.getConnection(url,user,password);

            //4.通过连接获取执行语句对象Statement
            stmt = conn.createStatement();

            String sql = "insert" +
                    " into t_teacher(teacher_id,teacher_name,teacher_birthday,teacher_sex) " +
                    " values(null,'name','birthday',default)";

            //5.执行增 删 改语句使用executeUpdate，返回几行受影响的int类型的值
            int n = stmt.executeUpdate(sql);

            if(n > 0){
                System.out.println("新增成功");
            }
            else{
                System.out.println("新增失败");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //6.关闭
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }
}
